package com.llkj.common.base;

import android.support.annotation.Nullable;
import android.support.v4.content.ContextCompat;
import android.support.v4.widget.SwipeRefreshLayout;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.support.v7.widget.StaggeredGridLayoutManager;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.llkj.common.R;
import com.llkj.common.mvp.BaseListPresenter;
import com.llkj.common.mvp.IListView;

import java.util.List;

/**
 * author: zhu.px
 * e-mail: 1271901673@qq.com
 * time  : 2018/5/8
 * desc  :
 */
public abstract class BaseListActivity<P extends BaseListPresenter, B extends BaseQuickAdapter> extends BaseActivity<P> implements IListView,
        BaseQuickAdapter.RequestLoadMoreListener {

    /**
     * 普通list布局
     */
    protected static final int LINEAR_LAYOUT_MANAGER = 0;
    /**
     * grid布局
     */
    protected static final int GRID_LAYOUT_MANAGER = 1;
    /**
     * 瀑布流布局
     */
    protected static final int STAGGERED_GRID_LAYOUT_MANAGER = 2;
    protected RecyclerView mRecyclerView;
    /**
     * 适配器
     */
    protected B mAdapter;
    /**
     * 默认为0单行布局
     */
    private int mListType = 0;
    /**
     * 排列方式默认垂直
     */
    private boolean mIsVertical = true;
    /**
     * grid布局与瀑布流布局默认行数
     */
    private int mSpanCount = 1;
    @Nullable
    private SwipeRefreshLayout mSwipeRefresh;

    @Override
    public void initView() {
        mRecyclerView = findViewById(R.id.recycler_view);
        mRecyclerView.setHasFixedSize(true);
        mRecyclerView.setItemAnimator(null);
        mSwipeRefresh = findViewById(R.id.swipe_layout);
        if (mSwipeRefresh != null) {
            mSwipeRefresh.setColorSchemeColors(ContextCompat.getColor(mContext, R.color.colorPrimary));
            mSwipeRefresh.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() {
                @Override
                public void onRefresh() {
                    loadData(true);
                }
            });
        }
        initSetting();
        chooseListType(mListType, mIsVertical);
    }

    @Override
    public void initData() {
        loadData(false);
    }

    protected void loadData(boolean isRefresh) {
        if (mPresenter != null) {
            mPresenter.loadFirstData(isRefresh);
        }
    }

    /**
     * 初始化各种状态处理
     * 在这个方法里处理的是recyclerview的所有的初始化，
     * 包括对他的展示形式，是list或grid或瀑布流
     */
    protected abstract void initSetting();

    /**
     * @param listType 选择布局种类
     */
    private void chooseListType(int listType, boolean isVertical) {
        switch (listType) {
            case LINEAR_LAYOUT_MANAGER:
                LinearLayoutManager linearLayoutManager = new LinearLayoutManager(mContext);
                linearLayoutManager.setOrientation(isVertical ? LinearLayoutManager.VERTICAL : LinearLayoutManager.HORIZONTAL);
                mRecyclerView.setLayoutManager(linearLayoutManager);
                break;
            case GRID_LAYOUT_MANAGER:
                GridLayoutManager gridLayoutManager = new GridLayoutManager(mContext, mSpanCount);
                gridLayoutManager.setOrientation(isVertical ? GridLayoutManager.VERTICAL : GridLayoutManager.HORIZONTAL);
                mRecyclerView.setLayoutManager(gridLayoutManager);
                break;
            case STAGGERED_GRID_LAYOUT_MANAGER:
                StaggeredGridLayoutManager staggeredGridLayoutManager = new StaggeredGridLayoutManager
                        (mSpanCount, isVertical ? StaggeredGridLayoutManager.VERTICAL : StaggeredGridLayoutManager.HORIZONTAL);
                mRecyclerView.setLayoutManager(staggeredGridLayoutManager);
                break;
            default:
                LinearLayoutManager layoutManager = new LinearLayoutManager(mContext);
                layoutManager.setOrientation(isVertical ? LinearLayoutManager.VERTICAL : LinearLayoutManager.HORIZONTAL);
                mRecyclerView.setLayoutManager(layoutManager);
                break;
        }
        mRecyclerView.setAdapter(mAdapter);
    }

    /**
     * 是否打开加载更多
     */
    protected void openLoadMore() {
        mAdapter.setOnLoadMoreListener(this, mRecyclerView);
    }

    @Override
    public void finishRefresh() {
        super.finishRefresh();
        if (mSwipeRefresh != null) {
            mSwipeRefresh.postDelayed(new Runnable() {
                @Override
                public void run() {
                    mSwipeRefresh.setRefreshing(false);
                    mSwipeRefresh.setEnabled(true);
                }
            }, 500);
        }
        mAdapter.setEnableLoadMore(true);
    }

    @Override
    public void showRefreshLoading() {
        super.showRefreshLoading();
        mAdapter.setEnableLoadMore(false);
    }

    @Override
    public void onLoadMoreRequested() {
        if (mSwipeRefresh != null) {
            mSwipeRefresh.setEnabled(false);
        }
        if (mPresenter != null) {
            mPresenter.loadMoreData();
        }
    }

    @Override
    public void loadMoreFail() {
        mAdapter.loadMoreFail();
    }

    /**
     * @param type       布局管理type
     * @param isVertical 是否是垂直的布局 ，true垂直布局，false横向布局
     */
    protected void setListType(int type, boolean isVertical) {
        mListType = type;
        mIsVertical = isVertical;
    }

    /**
     * 为grid样式和瀑布流设置横向或纵向数量
     *
     * @param spanCount 数量
     */
    protected void setSpanCount(int spanCount) {
        if (spanCount > 0) {
            mSpanCount = spanCount;
        }
    }

    @SuppressWarnings("unchecked")
    protected <T> void loadDataComplete(List<T> list) {
        if (mPresenter.getPageIndex() == 1) {
            mAdapter.setNewData(list);
            mAdapter.disableLoadMoreIfNotFullPage(mRecyclerView);
        } else {
            if (list == null || list.size() == 0) {
                mAdapter.loadMoreEnd(false);
            } else {
                mAdapter.addData(list);
                mAdapter.loadMoreComplete();
            }
        }
    }
}
